/*
 * Copyright 2011 Clean GWT
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleangwt.cell.client;

import java.util.ArrayList;
import java.util.List;

import com.cleangwt.user.shared.Option;
import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.cell.client.ValueUpdater;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.BrowserEvents;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.InputElement;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.safehtml.client.SafeHtmlTemplates;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;

/**
 * A cell used to render checkbox group.
 * 
 * @author Jess
 * @version 1.0
 * @since 1.0
 */
public class CheckGroupCell extends AbstractCell<List<String>> {

  interface Template extends SafeHtmlTemplates {
    @Template("<label><input type=\"checkbox\" value=\"{1}\" tabindex=\"-1\" checked>{0}</label>")
    SafeHtml checked(String option, String value);

    @Template("<label><input type=\"checkbox\" value=\"{1}\" tabindex=\"-1\">{0}</label>")
    SafeHtml unchecked(String option, String value);
  }

  private static Template template;

  private final List<Option> options;

  /**
   * Creates a check group cell with the given options.
   * 
   * @param options the options in the cell
   */
  public CheckGroupCell(List<Option> options) {
    super(BrowserEvents.CLICK);
    this.options = options;
    if (template == null) {
      template = GWT.create(Template.class);
    }
  }

  @Override
  public void onBrowserEvent(Context context, Element parent, List<String> value,
      NativeEvent event, ValueUpdater<List<String>> valueUpdater) {
    super.onBrowserEvent(context, parent, value, event, valueUpdater);
    String type = event.getType();
    if (BrowserEvents.CLICK.equals(type)) {
      InputElement input = event.getEventTarget().cast();
      if (valueUpdater != null && input.getValue() != null) {
        if (value == null) {
          value = new ArrayList<String>();
        }
        if (input.isChecked()) {
          value.add(input.getValue());
        } else {
          value.remove(input.getValue());
        }
        valueUpdater.update(value);
      }
    }
  }

  @Override
  public void render(Context context, List<String> value, SafeHtmlBuilder sb) {
    for (Option option : options) {
      if (value != null && value.contains(option.getValue())) {
        sb.append(template.checked(option.getName(), option.getValue()));
      } else {
        sb.append(template.unchecked(option.getName(), option.getValue()));
      }
    }
  }

}
